var path = require('path');
var express = require('express');
var config = require('./config');
var init = require('./init');
var util = require('./framework/util');
var logger = require('./framework/util').logger('startup');

var server = express();
//server.use(require('log4js').connectLogger(logger, {level: 'INFO'}));

server.configure(function () {
    server.use(express.urlencoded());
    server.use(express.json());
    server.use(require('connect-multiparty')());

    server.use(express.cookieParser());
    server.use(express.session({secret: 'apppress'}));

    server.use(express.methodOverride()); //checks req.body for HTTP method overrides
    server.use(server.router); //perform route lookup based on url and HTTP method
    server.use(express.static(config.web_root)); //Where to serve static content
    server.use(express.errorHandler({ dumpExceptions:true, showStack:true })); //Show all errors in development

});
express.static.mime.define({'application/font-woff': ['woff']});

//Start server
server.listen(config.port, function () {
    logger.info("Express server listening on port " + config.port + " in " + server.settings.env + " mode");
});
server.get('/api', function(req, res){
    res.send('Library API is running');
});


var loader = function(feature) {
    for (var i = 0; i < feature.infrastructure.length; i++) {
        var f = feature.infrastructure[i];
        require('./infrastructure/' + f + '/route')(server);
    }
    for(var e in feature.extensions){
        var m = eval('feature.extensions.' + e);
        for (var i = 0; i < m.length; i++) {
            var f = m[i];
            require('./public/ext/' + e + '/' + f + '/server/route')(server);
        }
    }
}

loader(config.feature);

init.init();